package com.goscom.app.deeset;

import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Referee extends Activity {

	private String strMerId = "";
	private DBAdapter mDB;
	private EditText edtName, edtRole, edtTel, edtConAdd;
	private String strActivity = "";
	private TextView txtTit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.referee);

		edtName = (EditText) findViewById(R.id.edtRefeName);
		edtRole = (EditText) findViewById(R.id.edtRefeRole);
		edtTel = (EditText) findViewById(R.id.edtRefeTel);
		edtConAdd = (EditText) findViewById(R.id.edtRefeConAdd);
		txtTit = (TextView) findViewById(R.id.txtRefeTit);

		mDB = new DBAdapter(Referee.this);
		mDB.open();

		getMerId();
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
			strActivity = getIntent().getStringExtra("activity");
			if (strActivity.equals("referee1")) {
				txtTit.setText("REFEREE 1");
			} else {
				txtTit.setText("REFEREE 2");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null) {
			ContentValues content = new ContentValues();
			content = mDB.queryDataByMerAddOther("REFEREE", strMerId, strActivity);
			if (content != null) {
				edtName.setText(content.getAsString("name"));
				edtRole.setText(content.getAsString("role"));
				edtTel.setText(content.getAsString("telephone"));
				edtConAdd.setText(content.getAsString("conadd"));
			}
		}
	}

	public void Logout(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(Referee.this, Login.class));
	}

	public void goPrevious(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		Intent intent;
		if (strActivity.equals("referee1")) {
			intent = new Intent(Referee.this, Vehicle.class);
			intent.putExtra("merid", strMerId);
		} else {
			intent = new Intent(Referee.this, Referee.class);
			intent.putExtra("merid", strMerId);
			intent.putExtra("activity", "referee1");
		}
		startActivity(intent);
	}

	public void goNext(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		Intent intent;
		if (strActivity.equals("referee1")) {
			intent = new Intent(Referee.this, Referee.class);
			intent.putExtra("merid", strMerId);
			intent.putExtra("activity", "referee2");
		} else {
			intent = new Intent(Referee.this, ContactEmergency.class);
			intent.putExtra("merid", strMerId);
		}
		startActivity(intent);
	}

	public void Save(View v) {
		saveDB();
		Toast.makeText(Referee.this, "Save Successful!", Toast.LENGTH_LONG)
				.show();
	}

	public void saveDB() {
		if (mDB.queryMerchandiser("REFEREE", strMerId) == 0) {
			insertDB();
		} else {
			mDB.deleteDataAddOther("REFEREE", strMerId, strActivity);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("name", edtName.getText().toString());
		content.put("role", edtRole.getText().toString());
		content.put("telephone", edtTel.getText().toString());
		content.put("conadd", edtConAdd.getText().toString());
		content.put("activity", strActivity);
		mDB.insertData("REFEREE", content);
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
